<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="IMEI" prop="serialNumberId">
        <el-input
          v-model="queryParams.serialNumberId"
          placeholder="请输入序列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="版本" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="设备状态" prop="configStatus">
        <el-select v-model="queryParams.deviceState" placeholder="请选择设备状态" clearable>
          <el-option
            v-for="item in deviceStatusOptions"
            :key="item.value"
            :label="item.value"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否配置" prop="configStatus">
        <el-select v-model="queryParams.configStatus" placeholder="请选择配置状态" clearable>
          <el-option
            v-for="item in configStatusOptions"
            :key="item.value"
            :label="item.value"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gatewayManagement:gatewayManagement:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gatewayManagement:gatewayManagement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gatewayManagement:gatewayManagement:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gatewayManagement:gatewayManagement:export']"
        >导出</el-button>
      </el-col> -->
      <!--    -->
      <el-col :span="2" :push = "4">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="small "
          @click="handleRefresh"
        >更新</el-button>
      </el-col>
      <el-col :span="2" :push = "4">
        <el-button
          type="danger"
          plain
          icon="el-icon-switch-button"
          size="small "
          :disabled="multiple"
          @click="handleRestart"
        >重启</el-button>
      </el-col>
      <el-col :span="2" :push = "4">
        <el-button
          type="danger"
          plain
          icon="el-icon-switch-button"
          size="medium"
          :disabled="multiple"
          @click="handleConfig"
        >配置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- <el-row :gutter="12">
      <el-col :span="8">
        <el-card shadow="always" :body-style="{ padding: '0', height: '60px' }">
          <div style="display: flex; align-items: center; justify-content: center; height: 100%; padding: 0 20px;">
            <div style="display: flex; align-items: center; margin-right: 12px;  background: #ecf5ff; border-radius: 4px; ">
              <i class="el-icon-refresh" style="font-size: 24px;" ></i>
            </div>
            <div style="display: flex; align-items: center; font-size: 16px;">
              更新
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="always" :body-style="{ padding: '0', height: '60px' }">
          <div style="display: flex; align-items: center; justify-content: center; height: 100%; padding: 0 20px;">
            <div style="display: flex; align-items: center; margin-right: 12px;  background: #ecf5ff; border-radius: 4px; ">
              <i class="el-icon-refresh" style="font-size: 24px;" ></i>
            </div>
            <div style="display: flex; align-items: center; font-size: 16px;">
              重启
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="never">
          配置
        </el-card>
      </el-col>
    </el-row> -->

    <el-table v-loading="loading" :data="gatewayManagementList" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="IMEI" align="center" prop="serialNumberId" />
      <el-table-column label="设备状态 " align="center" prop="deviceState" >
        <template slot-scope="scope">
          <el-tag
            :type="statusTagType(scope.row.deviceState)"
            effect="dark"
            style="min-width: 60px; text-align: center;"
          >
            {{ scope.row.deviceState || '未知状态' }}
          </el-tag>
        </template>

      </el-table-column>
      <el-table-column label="配置状态" align="center" prop="configStatus" />
      <el-table-column label="配置详情" align="center" prop="configInfoId" >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="success"
              icon="el-icon-warning-outline"
            >配置详情</el-button>
          </template>
      </el-table-column>
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gatewayManagement:gatewayManagement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gatewayManagement:gatewayManagement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改空调配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="序列号" prop="serialNumberId">
          <el-input v-model="form.serialNumberId" placeholder="请输入序列号" />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  对话框  -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      >
      <span>确定执行操作吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listGatewayManagement, getGatewayManagement, delGatewayManagement, addGatewayManagement, updateGatewayManagement ,
         updateMainboardData, restartMainboard, migrationService }
         from "@/api/gatewayManagement/gatewayManagement"

export default {
  name: "GatewayManagement",
  data() {
    return {
      dialogVisible: false,    //  对话框
      configStatusOptions: [
        {
          value: '已配置',
        },
        {
          value: '未配置',
        },
      ],
      deviceStatusOptions: [
        {
          value: '在线',
        },
        {
          value: '离线',
        },
        {
          value: '待机',
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //  选中 序列号
      serialNumberList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 空调配置表格数据
      gatewayManagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gatewayId: null,
        serialNumberId: null,
        deviceState: null,
        configStatus: null,
        configInfoId: null,
        version: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    statusTagType(status) {
        const statusMap = {
          '运行': 'success',
          '在线': 'success',
          '待机': 'info',
          '离线': 'danger',
          '故障': 'danger',
          '维修中': 'warning'
        };
        return statusMap[status] || '';
      },
    /** 更新*/
    handleRefresh(){
      this.dialogVisible = true;
    },
    /** 重启  */
    handleRestart(row){
      const gatewayIds = [row.gatewayId || this.ids].flat();
      const serialNumberIds = [row.serialNumberId || this.serialNumberList].flat();     //  序列号
      restartMainboard(gatewayIds).then(() => {
        this.$modal.msgSuccess("重启指令已发送");
      });
    },
    /**  配置 */
    handleConfig(row){
      const serialNumberIds = [row.serialNumberId || this.serialNumberList].flat();     //  序列号
      console.log("----配置---",serialNumberIds);
      migrationService(serialNumberIds).then(() =>{
        this.$modal.msgSuccess("配置指令已发送");
      })
    },
    //  弹窗关闭
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    dialogSure(){
      console.log("---点击了弹窗确定--");
      updateMainboardData().then(request => {
        this.$modal.msgSuccess("更新成功")
      });
    },
    /** 查询空调配置列表 */
    getList() {
      this.loading = true
      listGatewayManagement(this.queryParams).then(response => {
        this.gatewayManagementList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        gatewayId: null,
        serialNumberId: null,
        deviceState: null,
        configStatus: null,
        configInfoId: null,
        version: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log("---selection---",selection)
      this.serialNumberList = selection.map(item => item.serialNumberId);
      this.ids = selection.map(item => item.gatewayId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset()
    //   this.open = true
    //   this.title = "添加空调配置"
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const gatewayId = row.gatewayId || this.ids
       console.log("----修改---",gatewayId)
      getGatewayManagement(gatewayId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改配置"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gatewayId != null) {
            updateGatewayManagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addGatewayManagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gatewayIds = row.gatewayId || this.ids
      this.$modal.confirm('是否确认删除空调配置编号为"' + gatewayIds + '"的数据项？').then(function() {
        return delGatewayManagement(gatewayIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('gatewayManagement/gatewayManagement/export', {
    //     ...this.queryParams
    //   }, `gatewayManagement_${new Date().getTime()}.xlsx`)
    // }
  }
}
</script>
